package display.screens.faq {
	import fl.transitions.Tween;
	import fl.transitions.easing.Regular;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.ilovecode.events.FaqEvent;
	import org.ilovecode.mvc.View;
	
	/**
	 * Original provided by Mario Nikolaus.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class FaqScreenContent extends View {
		
		//Number of questions and answers defined as elements on stage.
		private var NUM_ELEMENTS:uint = 39;
		
		protected var faqItems:Array;
		
		/**
		 * Create instance of FaqScreenContent.
		 */
		public function FaqScreenContent() {
			super();
			//this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		override protected function construct():void
		{
			super.construct();
			
			faqItems = new Array();

			var screenXML:XML = controller.getScreenXML("faq");
			var fi:FaqItem;
			for each (var xml:XML in screenXML.descendants("item")) 
			{
				fi = new FaqItem(int(xml.@id), String(xml.question), String(xml.answer), "faqItem" + xml.@id);
				faqItems.push(fi);
				this.addChild(fi);
			}
		}
		
		override protected function initUI():void 
		{
			super.initUI();
			
			/*for(var i=1;i<NUM_ELEMENTS;i++){
				this["pitanje_"+i].addEventListener(MouseEvent.CLICK, answer);
				this["pitanje_"+i].buttonMode = true;
				if(i>1){
					this["pitanje_"+i].x = 0
					this["pitanje_"+i].y = this["pitanje_"+(i-1)].y + 25;
					this["maska_" + i].y = this["pitanje_" + (i - 1)].y + 35;
				}
			}*/
			
			var newY:Number = 0;
			for each (var fi:FaqItem in faqItems) 
			{
				fi.y = newY;
				newY += fi.minHeight;
				this.addChild(fi);
			}
			
		}	
		
		override protected function addListeners():void
		{
			super.addListeners();
			model.addEventListener(FaqEvent.SWITCH_ITEM, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			model.removeEventListener(FaqEvent.SWITCH_ITEM, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case FaqEvent.SWITCH_ITEM:
					handle_SwitchItem(FaqEvent(event).offset);
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		protected function handle_SwitchItem(offset:Number):void
		{
			this.parent.y -= offset;
			this.parent.dispatchEvent(new Event(Event.RESIZE));
		}		
		
		/**
		 * Show answer and tween other questions which are below clicked one.
		 * 
		 * @param	e	Click on question.
		 * @private
		 */
		/*private function answer(e:MouseEvent):void {
			var br:Number = Number(e.target.name.substr(8));
			var	prvi:Tween = new Tween(this["maska_"+br],"height",Regular.easeIn,17,this["pitanje_"+br].height,0.2,true);
			e.target.removeEventListener(MouseEvent.CLICK, answer);
			e.target.addEventListener(MouseEvent.CLICK, unanswer);
			for(var i=br;i<NUM_ELEMENTS;i++){
				var j=i+1;
				if (j < NUM_ELEMENTS) {
					var proba:Tween = new Tween(this["pitanje_"+j],"y",Regular.easeIn,this["pitanje_"+j].y,this["pitanje_"+j].y+this["pitanje_"+br].height-20,0.1,true);
					var proba2:Tween = new Tween(this["maska_" + j], "y", Regular.easeIn, this["maska_" + j].y, this["maska_" + j].y + this["pitanje_" + br].height - 20, 0.1, true);
				}
			}
		}*/
		
		/**
		 * Hide answer and tween other questions which are below clicked one.
		 * 
		 * @param	e	Click on question.
		 * @private
		 */
		/*private function unanswer(e:MouseEvent):void {
			var br:Number = Number(e.target.name.substr(8));
			var	prvi:Tween = new Tween(this["maska_"+br],"height",Regular.easeIn,this["pitanje_"+br].height,17,0.2,true);
			e.target.removeEventListener(MouseEvent.CLICK, unanswer);
			e.target.addEventListener(MouseEvent.CLICK, answer);
			for(var i=br;i<NUM_ELEMENTS;i++){
				var j=i+1;
				if (j < NUM_ELEMENTS) {
					var proba:Tween = new Tween(this["pitanje_" + j], "y", Regular.easeIn, this["pitanje_" + j].y, this["pitanje_" + j].y - this["pitanje_" + br].height + 20, 0.2, true);
					var proba2:Tween = new Tween(this["maska_" + j], "y", Regular.easeIn, this["maska_" + j].y, this["maska_" + j].y - this["pitanje_" + br].height + 20, 0.2, true);
				}
			}
		}*/
		
	}

}